import useWebSocket from '@/hooks/UseWebsocket';
import { Button, message } from 'antd'
import React, { useEffect, useState } from 'react'
import LostCardTable from '../ReportLostCard/LostCardTable';
import AdminCardmodel from './components/AdminCardmodel';

const ReturnThecard: React.FC = () => {


    const [sendMsg, setsendMsg] = useState<any>(''); // 发送消息
    const [CardMessage, setCardMessage] = useState(); // 接受消息
    const [type, setType] = useState<string>(''); // 成功后的提示消息
    const [getCardNum, setgetCardNum] = useState<string>(''); // 读设置卡 卡号
    const [_, sendMessage, lastMessage, isConnected] = useWebSocket({
        url: 'ws://127.0.0.1:9909',
        onOpen: () => {
            if (sendMsg) {
                sendMessage(sendMsg);
            }
        },
        onClose: () => {
            //连接关闭
            console.log('WebSocket disconnected');
        },
        onError: (event) => {
            //连接异常
            console.error('WebSocket error:', event);
        },
        onMessage: (onmsg) => {
            //收到消
            setCardMessage(JSON.parse(onmsg));
        },
    });
    useEffect(() => {
        if (isConnected && sendMsg) {
            sendMessage(sendMsg);
        }
    }, [sendMsg, isConnected]);
    useEffect(() => {
        if (CardMessage) {
            if (type == '读卡成功1') {
                if (JSON.parse(lastMessage).cardid) {
                    setgetCardNum(JSON.parse(lastMessage).cardid);

                    if (JSON.parse(lastMessage).result == 0) {
                        // AdminCard(JSON.parse(lastMessage).cardid)

                    } else {
                        message.error(JSON.parse(lastMessage).msg);
                    }


                } else {
                    message.error(JSON.parse(lastMessage).msg);
                }
            }
        }
    }, [CardMessage]);

    const [useflag, setuseflag] = useState(false)

    // const AdminCard = (card: string) => {
    //     let data = {
    //         "uuid": "clearcard",
    //         "cardclass": "clearcard",
    //         "count": "0"
    //     }
    //     setsendMsg(data)
    //     message.success('退卡成功');
    // }

    return (
        <div>

            {
                useflag && <AdminCardmodel useflag={useflag} />
            }
            <LostCardTable msg={"TCard"} setsendMsg={setsendMsg} setType={setType} />
        </div>
    )
}

export default ReturnThecard
